package cn.anquing.study.qlexpress.demo;

import cn.anquing.study.qlexpress.beans.BeanExample;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;

/**
 * 绑定java类或者对象的method
 * @author wangquanqing
 * @since 2021/2/23 10:39
 */
public class Demo4 {

    public static void main(String[] args) throws Exception {
        ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> context = new DefaultContext<String, Object>();

        runner.addFunctionOfClassMethod("取绝对值", Math.class.getName(), "abs",
                new String[] { "double" }, null);
        runner.addFunctionOfClassMethod("转换为大写", BeanExample.class.getName(),
                "upper", new String[] { "String" }, null);

        runner.addFunctionOfServiceMethod("打印", System.out, "println",new String[] { "String" }, null);
        runner.addFunctionOfServiceMethod("contains", new BeanExample(), "anyContains",
                new Class[] { String.class, String.class }, null);

        String exp1 = "取绝对值(-100)";
        String exp2 = "转换为大写(\"hello world\")";
        String exp3 = "打印(\"你好吗？\")";
        String exp4 = "contains(\"helloworld\",\"aeiou\")";

        Object r1 = runner.execute(exp1, context, null, false, false);
        Object r2 = runner.execute(exp2, context, null, false, false);
        runner.execute(exp3, context, null, false, false);
        Object r4 = runner.execute(exp4, context, null, false, false);

        System.out.println(r1);
        System.out.println(r2);
        System.out.println(r4);

        //运行结果：
//        你好吗？
//        100.0
//        HELLO WORLD
//        true

    }
}
